#include "view_heart_rate.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "view_common.h"
#include "Motor.h"
#include "view.h"
#include "times/view_times.h"
#include "sports_mode.h"
#include "app_trace.h"
#define LOG(fmt, ...) app_trace_log(fmt "\r\n", ##__VA_ARGS__)

void view_heart_rate_animation(view_heart_rate_param const *param)
{
    if (param == 0 || param->file_index_0 < DISP_LFS_FILE_INDEX_BEGIN || param->file_index_0 >= DISP_LFS_FILE_INDEX_END || param->file_index_1 < DISP_LFS_FILE_INDEX_BEGIN ||
        param->file_index_1 >= DISP_LFS_FILE_INDEX_END)
    {
        return;
    }

    if (!param->is_wear)
    {
        _disp_show_flash_img(param->file_index_0, 66, 64, FILE_IMG_T112_not_wear_66_64);
        // _disp_show_flash_img(param->file_index_1, 24, 147, FILE_IMG_T113_not_wear_text_24_147);
        disp_show_localize((disp_little_fs_file_index_t)param->file_index_1, localize_string_key_AlertNotWear, param->temp_index);
        return;
    }
    int x = 70;
    int y = 38;
    img_gif_com_t const img_cfg = {
        .gif_count = 3,
        .img[0].x = x,
        .img[0].y = y,
        .img[0].sn = FILE_IMG_H101_heartrate_1_70_38,
        .img[1].x = x,
        .img[1].y = y,
        .img[1].sn = FILE_IMG_H102_heartrate_2_70_38,
        .img[2].x = x,
        .img[2].y = y,
        .img[2].sn = FILE_IMG_H103_heartrate_3_70_38,
    };
    _disp_show_flash_img_gif_all(&img_cfg, param->file_index_0);
    int unit_width = 0;
    if (param->unit_width > 0)
    {
        unit_width = param->unit_width;
    }
    int x_center = 120 - unit_width / 2;
    int const font_width = 15;
    int const num_width = font_width + 4;
    int const num_height = 37;
    //    int unit_height = num_height;
    //    if (param->unit_height > 0)
    //    {
    //        unit_height = param->unit_height;
    //    }
    y = 145;
    if (param->num < 0)
    {
        x = x_center - num_width;
        if (disp_ctrl.auto_refresh_count % 2 == 0)
        {
            int x1 = x;
            int y1 = y + num_height - num_height / 2;
            int x2 = x1 + font_width;
            int y2 = y1 + 1;
            disp_draw_rect_color(x1, y1, x2, y2, RGB565_COLOR_WHITE);

            x1 += num_width;
            x2 = x1 + font_width;
            disp_draw_rect_color(x1, y1, x2, y2, RGB565_COLOR_WHITE);
        }
        else
        {
        }
        x += num_width * 2;
    }
    else
    {
        if (param->num_digit > 0)
        {
            x = disp_draw_user_image_num_mid_ignore_with_digit(x_center, y, RGB565_COLOR_BLACK, param->num, num_width, param->num_digit, num_15x37_TAB);
        }
        else
        {
            x = disp_draw_user_image_num_mid_ignore(x_center, y, RGB565_COLOR_BLACK, param->num, num_width, num_15x37_TAB);
        }
    }
    y += num_height - param->unit_height;
    _disp_show_flash_img(param->file_index_1, x, y, param->unit_index);
}
void view_heart_rate_display(int view)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.com_data.i_temp[0] = hr_get_hr_data();
        disp_ctrl.com_data.i_temp[1] = hr_if_band_wear();
    }
    int num = disp_ctrl.com_data.i_temp[0];
    if (num == 0)
    {
        num = -1;
    }
    view_heart_rate_param const param = {
        .is_wear = disp_ctrl.com_data.i_temp[1],
        .num = num,
        .num_digit = 0,
        .unit_index = FILE_IMG_H104_heartrate_unit_bpm_129_159,
        .file_index_0 = DISP_LFS_FILE_INDEX_0,
        .file_index_1 = DISP_LFS_FILE_INDEX_1,
        .unit_height = 22,
        .unit_width = 39,
        .temp_index = 50,
    };
    view_heart_rate_animation(&param);
    // int const unit_width = 39;
}
view_event_result_t view_heart_rate_event(int view, event_param_t const *param)
{
#if 0
#warning debug
    switch (param->event)
    {
    case event_view_entry:
        disp_ctrl.auto_close_delay = 60000;
        hr_test_start(HR_CTRL_TYPE_HR_TEST);
        return view_event_result_handled;
    case event_view_re_screen_on:
        disp_ctrl.auto_close_delay = 60000;
        return view_event_result_handled;
    case event_view_exit:
        hr_test_stop(HR_CTRL_TYPE_HR_TEST);
        return view_event_result_handled;
    }
    return view_event_result_handled;
#else
    switch (param->event)
    {
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
        return view_event_result_handled;
    case event_view_entry:
        disp_ctrl.auto_close_delay = 60000;
        hr_test_start(HR_CTRL_TYPE_HR_TEST);
        return view_event_result_handled;
    case event_view_re_screen_on:
        disp_ctrl.auto_close_delay = 60000;
        return view_event_result_handled;
    case event_view_exit:
        hr_test_stop(HR_CTRL_TYPE_HR_TEST);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
#endif
}
